<?php
/* 
 * class.userset.php
 * 我的家园-用户信息
 * @author samuel
 * @date 2009-05-24
 */
include_once 'class.myhomeBase.php';
class myhome_userset extends myhomeBase {

    function  __construct($core) {
        parent::__construct();
        //参数处理
        $this->step = $this->params['step'] ? strtolower(trim($core->params['step'])) : 'index';
	    $this->user_id = $this->params['uid'] ? (int)$this->params['uid'] : 1;
	    if(!$this->user_id){
		    $this->error = "用户ID不合法！";
		    $this->jumpError();
		    exit;
	    }

        $run = "run_" . $this->step;
        
        //header("Content-type: text/vnd.wap.wml");
        $this->$run();
    }

    function run_index(){
    	//WAP页面
	    //header("Content-type: text/vnd.wap.wml");
	    //TITLE
	    $page_title = "我的家园-用户设置";
	    $content = "";
	    $content .= "<img src='' alt='头像' /><br/>";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?uid=".$this->user_id."'>动态</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userinfo&amp;uid=".$this->user_id."'>信息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;uid=".$this->user_id."'>设置</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=msg&amp;uid=".$this->user_id."'>消息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."'>收藏</a>｜<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;uid=".$this->user_id."'>好友</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=pubSeller&amp;uid=".$this->user_id."'>发布商家</a>｜<a href='".$_SERVER['PHP_SELF']."?act=pubDiscount&amp;uid=".$this->user_id."'>发布优惠</a><br/>";
	
	    //获取用户个人设置
	    $userset = $this->getUserCustomeInfo($this->user_id);
	    //var_dump($userset);

        $content .= "我的城市<br/>";
        if($userset['CustomDiscountIs'] == '1'){
            $content .= "优惠信息显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomDiscountIs&amp;uid=".$this->user_id."&amp;v=0'>关</a><br/>";
        }else{
            $content .= "优惠信息显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomDiscountIs&amp;uid=".$this->user_id."&amp;v=1'>开</a><br/>";
        }
        if($userset['CustomSetsIs'] == '1'){
            $content .= "套餐信息显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomSetsIs&amp;uid=".$this->user_id."&amp;v=0'>关</a><br/>";
        }else{
            $content .= "套餐信息显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomSetsIs&amp;uid=".$this->user_id."&amp;v=1'>开</a><br/>";
        }
        if($userset['CustomtypeIs'] == '1'){
            $content .= "商家类别显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomtypeIs&amp;uid=".$this->user_id."&amp;v=0'>关</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=Customtype&amp;uid=".$this->user_id."'>序</a><br/>";
        }else{
            $content .= "商家类别显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomtypeIs&amp;uid=".$this->user_id."&amp;v=1'>开</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=Customtype&amp;uid=".$this->user_id."'>序</a><br/>";
        }
        if($userset['CustomqyIs'] == '1'){
            $content .= "地区类别显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomqyIs&amp;uid=".$this->user_id."&amp;v=0'>关</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=Customqy&amp;uid=".$this->user_id."'>序</a><br/>";
        }else{
            $content .= "地区类别显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomqyIs&amp;uid=".$this->user_id."&amp;v=1'>开</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=Customqy&amp;uid=".$this->user_id."'>序</a><br/>";
        }
        if($userset['CustomsqIs'] == '1'){
            $content .= "商圈显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomsqIs&amp;uid=".$this->user_id."&amp;v=0'>关</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=Customsq&amp;uid=".$this->user_id."'>序</a><br/>";
        }else{
            $content .= "商圈显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomsqIs&amp;uid=".$this->user_id."&amp;v=1'>开</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=Customsq&amp;uid=".$this->user_id."'>序</a><br/>";
        }
        if($userset['CustomCytypeIs'] == '1'){
            $content .= "餐饮类别显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomCytypeIs&amp;uid=".$this->user_id."&amp;v=0'>关</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=CustomCytype&amp;uid=".$this->user_id."'>序</a><br/>";
        }else{
            $content .= "餐饮类别显示:<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_do&amp;ope=CustomCytypeIs&amp;uid=".$this->user_id."&amp;v=1'>开</a> <a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order&amp;ope=CustomCytype&amp;uid=".$this->user_id."'>序</a><br/>";
        }
            

	    $content .= "<br/><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }
    
    function run_set_do(){
        $page_title = "我的家园－自定义设置";
    	$backurl = $_SERVER['PHP_SELF'] . '?act=userset&uid=' . $this->user_id;
        $ope = $this->params['ope'];
        $val = $this->params['v'];
        $res = $this->setUserCustomeInfo($this->user_id, $ope, $val);
        
        if($res){
            $content = "设置成功！";
        }else{
            $content = "设置失败！";
        }

    	$this->jumpPrompt($content, $page_title, $backurl);

    }
    
    function run_set_order(){
        $ope = $this->params['ope'];
        $page_title = "我的家园-用户设置";
	    $content = "";
	    $content .= "<img src='' alt='头像' /><br/>";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?uid=".$this->user_id."'>动态</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userinfo&amp;uid=".$this->user_id."'>信息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;uid=".$this->user_id."'>设置</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=msg&amp;uid=".$this->user_id."'>消息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."'>收藏</a>｜<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;uid=".$this->user_id."'>好友</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=pubSeller&amp;uid=".$this->user_id."'>发布商家</a>｜<a href='".$_SERVER['PHP_SELF']."?act=pubDiscount&amp;uid=".$this->user_id."'>发布优惠</a><br/>";
	    
	
	    //获取用户个人设置
	    $userset = $this->getUserCustomeInfo($this->user_id);
	    
	    $order = $userset[$ope];

        if(!$order){//没有设置过
            switch($ope){
                case 'Customtype':
                    $subtitle = "商家类别排序：";
                    $sql = "SELECT `SellerClassID` as id, `SellerClassName` as name FROM `fun_seller_class` WHERE `SellerClassOwner`=0";
                    break;
                case 'Customqy':
                    $subtitle = "商家地区排序：";
                    $sql = "SELECT `SellerAreaID` as id, `SellerAreaNameCN` as name FROM `fun_seller_area`";
                    break;
                case 'Customsq':
                    $subtitle = "商家商圈排序：";
                    $sql = "SELECT `SellerCBDID` as id, `SellerCBDNameCN` as name FROM `fun_seller_cbd`";
                    break;
                case 'CustomCytype':
                    $subtitle = "商家餐饮类别排序：";
                    $sql = "SELECT `SellerClassID` as id, `SellerClassName` as name FROM `fun_seller_class` WHERE `SellerClassOwner`=1";
                    break;
            }
        }else{
            switch($ope){
                case 'Customtype':
                    $subtitle = "商家类别排序：";
                    $sql = "SELECT `SellerClassID` as id FROM `fun_seller_class` WHERE `SellerClassOwner`=0";
                    $all_ids = array();
                    foreach ( $this->core->db->get_col($sql,0) as $id ){
                        $all_ids[] = $id;
                    }
                    $arr_orderids = explode(',', $order);
                    $order_ids = array_merge($arr_orderids, $all_ids);
                    $order_ids = array_unique($order_ids);
                    $str_orderids = implode(',', $order_ids);
                    $sql = "SELECT `SellerClassID` as id, `SellerClassName` as name FROM `fun_seller_class` WHERE `SellerClassOwner`=0 ORDER BY FIELD(id,".$str_orderids.")";
                    break;
                case 'Customqy':
                    $subtitle = "商家地区排序：";
                    $sql = "SELECT `SellerAreaID` as id  FROM `fun_seller_area`";
                    $all_ids = array();
                    foreach ( $this->core->db->get_col($sql,0) as $id ){
                        $all_ids[] = $id;
                    }
                    $arr_orderids = explode(',', $order);
                    $order_ids = array_merge($arr_orderids, $all_ids);
                    $order_ids = array_unique($order_ids);
                    $str_orderids = implode(',', $order_ids);
                    $sql = "SELECT `SellerAreaID` as id, `SellerAreaNameCN` as name FROM `fun_seller_area` ORDER BY FIELD(id,".$str_orderids.")";
                    break;
                case 'Customsq':
                    $subtitle = "商家商圈排序：";
                    $sql = "SELECT `SellerCBDID` as id FROM `fun_seller_cbd`";
                    $all_ids = array();
                    foreach ( $this->core->db->get_col($sql,0) as $id ){
                        $all_ids[] = $id;
                    }
                    $arr_orderids = explode(',', $order);
                    $order_ids = array_merge($arr_orderids, $all_ids);
                    $order_ids = array_unique($order_ids);
                    $str_orderids = implode(',', $order_ids);
                    $sql = "SELECT `SellerCBDID` as id, `SellerCBDNameCN` as name FROM `fun_seller_cbd` ORDER BY FIELD(id,".$str_orderids.")";
                    break;
                case 'CustomCytype':
                    $subtitle = "商家餐饮类别排序：";
                    $sql = "SELECT `SellerClassID` as id FROM `fun_seller_class` WHERE `SellerClassOwner`=1";
                    $all_ids = array();
                    foreach ( $this->core->db->get_col($sql,0) as $id ){
                        $all_ids[] = $id;
                    }
                    $arr_orderids = explode(',', $order);
                    $order_ids = array_merge($arr_orderids, $all_ids);
                    $order_ids = array_unique($order_ids);
                    $str_orderids = implode(',', $order_ids);
                    $sql = "SELECT `SellerClassID` as id, `SellerClassName` as name FROM `fun_seller_class` WHERE `SellerClassOwner`=1 ORDER BY FIELD(id,".$str_orderids.")";
                    break;
            }
        }

        $content .= $subtitle."<br/>";

        $rs = $this->core->db->get_results($sql);
        if($rs){
            foreach($rs as $cate){
                $content .= $cate->name;
                $content .= "<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order_do&amp;uid=".$this->user_id."&amp;ope=".$ope."&amp;ov=".$order."&amp;cv=".$cate->id."&amp;type=add'>↑</a> ";
                $content .= "<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;step=set_order_do&amp;uid=".$this->user_id."&amp;ope=".$ope."&amp;ov=".$order."&amp;cv=".$cate->id."&amp;type=del'>↓</a><br/>";
            }
        }

	    $content .= "<br/><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }
    
    function run_set_order_do(){
        $page_title = "我的家园－自定义排序设置";
    	$backurl = $_SERVER['PHP_SELF'] . '?act=userset&uid=' . $this->user_id;
    	$type = $this->params['type'];
        $ope = $this->params['ope'];
        $ov = $this->params['ov'];
        $cv = $this->params['cv'];
        
        $new_ov = "";
        if($ov){//已经设置过排序
            $new_ov = $this->_ope_order($ov, $cv, $type);
        }else{
            $new_ov = $cv;
        }
        
        $res = $this->setUserCustomeInfo($this->user_id, $ope, $new_ov);
        
        if($res){
            $content = "设置成功！";
        }else{
            $content = "设置失败！";
        }

    	$this->jumpPrompt($content, $page_title, $backurl);
    }
    
    private function _ope_order($ov,$cv,$type){
        $arr_orders = @explode(',', $ov);
        $idx = @array_search($cv, $arr_orders);
        if($idx === false){
            $n_ov = $ov . "," . $cv;
            return $n_ov;
        }
        if($type == 'add'){
            $n_idx = $idx - 1 <= -1 ? 0 : ($idx - 1);
        }else{
            $n_idx = $idx + 1 >= count($arr_orders) ? $idx : ($idx + 1);
        }
        $tmp_other = $arr_orders[$n_idx];
        $arr_orders[$n_idx] = $cv;
        $arr_orders[$idx] = $tmp_other;
        $n_ov = @implode(',', $arr_orders);
        return $n_ov;
    }
}


?>
